//package com.ct.boot.launch.config;
//
//import com.atomikos.jdbc.AtomikosDataSourceBean;
//import com.mysql.cj.jdbc.MysqlXADataSource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
//import org.springframework.boot.context.properties.ConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.DependsOn;
//import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
//import org.springframework.orm.jpa.JpaVendorAdapter;
//import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
//
//import javax.sql.DataSource;
//import java.sql.SQLException;
//import java.util.HashMap;
//
///**
// * @Auther: Charles chen
// * @Date: 2020/7/18 11:39
// * @Description:
// */
//@Configuration
//@DependsOn("transactionManager")
//@EnableJpaRepositories(basePackages = "com.ct.boot.launch.dao.testdb2",
//        entityManagerFactoryRef = "secondaryEntityManager",
//        transactionManagerRef = "transactionManager"
//)
//public class JPASecondaryConfig {
//    @Autowired
//    private JpaVendorAdapter jpaVendorAdapter;
//
//    //secondary
//    @Bean(name = "secondaryDatasourceProperties")
//    @ConfigurationProperties(prefix = "spring.datasource.secondary")
//    public DataSourceProperties secondaryDatasourceProperties(){
//        return new DataSourceProperties();
//    }
//
//
//    //把数据源转换成AtomikosDataSourceBean
//    @Bean(name = "secondaryDatasource",initMethod ="init",destroyMethod = "close")
//    @ConfigurationProperties(prefix = "spring.datasource.secondary")
//    public DataSource secondaryDatasource() throws SQLException {
//        MysqlXADataSource mysqlXADataSource=new MysqlXADataSource();
//        mysqlXADataSource.setUrl(secondaryDatasourceProperties().getUrl());
//        mysqlXADataSource.setPinGlobalTxToPhysicalConnection(true);
//        mysqlXADataSource.setPassword(secondaryDatasourceProperties().getPassword());
//        mysqlXADataSource.setUser(secondaryDatasourceProperties().getUsername());
//        AtomikosDataSourceBean xaDataSource=new AtomikosDataSourceBean();
//        xaDataSource.setXaDataSource(mysqlXADataSource);
//        xaDataSource.setUniqueResourceName("secondary");
//        xaDataSource.setBorrowConnectionTimeout(60);
//        xaDataSource.setMaxPoolSize(20);
//        return xaDataSource;
//    }
//    //实体管理器
//    @Bean(name = "secondaryEntityManager")
//    @DependsOn("transactionManager")
//    public LocalContainerEntityManagerFactoryBean secondaryEntityManager() throws SQLException {
//        HashMap<String, Object> properties=new HashMap<>();
//        properties.put("hibernate.transaction.jta.platform",AtomikosJtaPlatform.class.getName());
//        properties.put("javax.persistence.transactionType","JTA");
//        LocalContainerEntityManagerFactoryBean entityManager=new LocalContainerEntityManagerFactoryBean();
//        entityManager.setJtaDataSource(secondaryDatasource());
//        entityManager.setJpaVendorAdapter(jpaVendorAdapter);
//        entityManager.setPackagesToScan("com.ct.boot.launch.dao.testdb2");//指定实体存放的位置
//        entityManager.setJpaPropertyMap(properties);
//        return entityManager;
//    }
//}
